package cn.ex.ymcc.mq;


import cn.ex.ymcc.constants.BaseConstants;
import cn.ex.ymcc.domain.Course;
import cn.ex.ymcc.vo.EmailSend2MQ;
import cn.ex.ymcc.vo.SMSSend2MQ;
import cn.ex.ymcc.vo.StationMessageSend2MQ;
import cn.ex.ymcc.prpo.CourseMessageProp;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Slf4j
public class CourseMessageSender {

    @Autowired
    private CourseMessageProp courseMessageProp;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    //课程发布，消息推送
    public  void sendMessage(Course course) {
        //1.短信推送
        //消息内容
        String smsContent = String.format(courseMessageProp.getSmsContent(), course.getId(),course.getName());
        log.info("课程服务，推送短信消息 {}",smsContent);
        //手机号
        rocketMQTemplate.sendOneWay(
                BaseConstants.MessageMQ.TOPIC_MESSAGE +":"+ BaseConstants.MessageMQ.TAGS_SMS_NAME,
                JSON.toJSONString( new SMSSend2MQ(Arrays.asList(1L),"课程发布",smsContent ))
        );


        rocketMQTemplate.asyncSend(
                BaseConstants.MessageMQ.TOPIC_MESSAGE + ":" + BaseConstants.MessageMQ.TAGS_SMS_NAME,
                JSON.toJSONString(new SMSSend2MQ(Arrays.asList(1L), "课程发布", smsContent)), new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        System.out.println(sendResult);
                    }

                    @Override
                    public void onException(Throwable e) {
                        e.printStackTrace();
                    }
                }
        );

        //2.邮件推送
        String emailContent = String.format(courseMessageProp.getEmailContent(), course.getId(),course.getName());

        log.info("课程服务，推送邮件消息 {}",emailContent);

        rocketMQTemplate.sendOneWay(
                BaseConstants.MessageMQ.TOPIC_MESSAGE +":"+ BaseConstants.MessageMQ.TAGS_EMAIL_NAME,
                JSON.toJSONString( new EmailSend2MQ(Arrays.asList(1L),"课程发布",emailContent ))
        );

        //3.站内信推送
        String stationContent = String.format(courseMessageProp.getStationContent(), course.getId(),course.getName());
        log.info("课程服务，推送站内信消息 {}",stationContent);

        Message<String> message = MessageBuilder.withPayload(
                JSON.toJSONString(new StationMessageSend2MQ("课程发布啦", stationContent, Arrays.asList(1l, 2l), "系统消息"))).build();

        rocketMQTemplate.sendOneWay(
                BaseConstants.MessageMQ.TOPIC_MESSAGE +":"+ BaseConstants.MessageMQ.TAGS_STATION_MESSAGE,
                message);
    }
}
